<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var wordBreak = function (s, wordDict) {
                let bagSize = s.length
                let dp = new Array(bagSize + 1).fill(false)
                dp[0] = true
                for (let j = 0; j <= bagSize; j++) {
                    for (let i = 0; i < wordDict.length; i++) {
                        if (j >= wordDict[i].length) {
                            //去除掉已经在字典里的 wordDict[i]。
                            //j代表的是长度，所以下标为j-1
                            //temp是从wordDict[i].length的下一位开始 因此为  (j-wordDict[i].length-1)+1
                            //终止条件为到j-1  因为要包含最后一位需要+1 因此为j-1 + 1
                            let temp = s.slice(j - wordDict[i].length, j)
                            if (wordDict.includes(temp) && dp[j - wordDict[i].length]) {
                                dp[j] = true
                            }
                        }
                    }
                }
                return dp[bagSize]
            }
        </script>
    </body>
</html>
